﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common;
using BySoft.Server.Common.Enum;
using BySoft.Server.Common.WebCommon;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    /// <summary>
    /// SysUser
    /// </summary>
    public partial class SysUser
    {

         #region GetSupervisorByID
        /// <summary>
        /// 取得用户的上级用户实体
        /// </summary>
        /// <param name="id">实体关键字</param>
        /// <returns></returns>
        public static SysUserEntity GetSupervisorByID(string id)
        {
            return SysUserDA.GetSupervisorByID(id);
        }

        #endregion


        #region 获取用户所在部门中某职位的所有用户
        /// <summary>
        /// 根据用户ID，获取该用户所在部门中某职位的所有用户
        /// </summary>
        /// <param name="id">实体关键字</param>
        /// <param name="jobtitleid">职位代号</param>
        /// <returns></returns>
        public static List<SysUserEntity> GetOnePositionUsersByUserID(string id, string jobtitleid)
        {
            string strSql = "SELECT " + SysUserTable.C_DEPARTMENTID + " FROM " + SysUserTable.C_TABLENAME;
            WhereBuilder wb = new WhereBuilder(strSql);
            wb.AddCondition(SysUserTable.C_USERID + "='" + id.Replace("'", "''") + "'");
            string strCondition = string.Format(SysUserTable.C_DEPARTMENTID + " IN ( {0}) AND " + SysUserTable.C_JOBTITLEID + "='{1}' and " + SysUserTable.C_STATUS + "='1'", wb.SQLString, jobtitleid);

            return SysUser.GetByCondition(strCondition);
        }

        #endregion

        #region GetAll
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>返回所有数据</returns>
        public static DataTable GetAllUserInfo(string userName, string userId, string status,
                                                   int pageIndex,
                                                   int pageSize,
                                                   out int count)
        {
            return SysUserDA.GetAllUserInfo( userName, userId, status, pageIndex, pageSize, out count);
        }

        public static DataTable GetAllUserInfo(string usertype, string userName, string userId, string status,
                                                   int pageIndex,
                                                   int pageSize,
                                                   out int count)
        {
            return SysUserDA.GetAllUserInfo(usertype, userName, userId, status, pageIndex, pageSize, out count);
        }
        #endregion


        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int ChangeUserStatus(string userId, string status)
        {
            try
            {
                return SysUserDA.ChangeUserStatus(userId, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int ResetPwd(string userId, string pwd)
        {
            try
            {
                return SysUserDA.ResetPwd(userId, pwd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        ///  获得用户相关信息
        /// </summary>
        /// <param name="loginId">LoginId</param>
        /// <returns></returns>
        public static SysUserEntity GetUserLoginInfo(string loginId)
        {
            try
            {
                return SysUserDA.GetUserLoginInfo(loginId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 获取下属
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetLowerUser(string userId)
        {
            return SysUserDA.GetLowerUser(userId);
        }

        #region 获取用户名

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetName(string userId)
        {
            Object userName = DataAccessCommon.SelectSingleObject(string.Format("SELECT " + SysUserTable.C_USERNAME + " FROM " + SysUserTable.C_TABLENAME + " WHERE " + SysUserTable.C_USERID + " = '{0}'", userId));
            if (userName != null)
                return userName.ToString();
            else
                return null;
        }

        #endregion

        #region 获得员工类型
        /// <summary>
        /// 获得员工类型
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetEmployeeType(string userId)
        {
            Object userName = DataAccessCommon.SelectSingleObject(string.Format("SELECT " + SysUserTable.C_EMPLOYEETYPE + " FROM " + SysUserTable.C_TABLENAME + " WHERE " + SysUserTable.C_USERID + " = '{0}'", userId));
            if (userName != null)
                return userName.ToString();
            else
                return null;
        }
        #endregion

        #region  根据用户名称获取用户
        /// <summary>
        ///  根据用户名称获取用户,精确查找
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public static List<SysUserEntity> GetUserByName(string name)
        {
            return GetByCondition(" where " + SysUserTable.C_USERNAME + "='" + name.Replace("'", "''") + "'");
        }
        #endregion

        /// <summary>
        /// 限制可配权限人数不能超过5个
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static Boolean CanUserRole(string userId)
        {
            return SysUserDA.CanUserRole(userId);
        }

        public static DataTable Select()
        {
            return SysUserDA.Select();
        }

        public static DataTable Select(string strUserID, string strUserName,
                                                      int pageIndex,
                                                      int pageSize,
                                                      out int count)
        {
            return SysUserDA.Select(strUserID, strUserName, pageIndex, pageSize, out count);
        }

        public static bool IsUser(string strCode)
        {
            return SysUserDA.IsUser(strCode);
        }

        public static bool Del(string strCode)
        {
            bool b = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    SysUserDA.Del(strCode, broker);
                    broker.Commit();
                    b = true;
                }
                catch
                {
                    b = false;
                    broker.Rollback();
                    throw;
                }
            }
            return b;
        }


        public static void UserProcessSaveOrUpdate(string strUserID,List<SysUserprocessEntity> lstbody, string Type)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (Type == "Save")
                    {
                        for (int i = 0; i < lstbody.Count; i++)
                        {
                            DataAccessCommon.Insert(lstbody[i], true, broker);
                        }
                    }
                    else
                    {
                        SysUserDA.DeleteDetail(strUserID, broker);
                        for (int i = 0; i < lstbody.Count; i++)
                        {
                            DataAccessCommon.Insert(lstbody[i], true, broker);
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectUserProcess(string strCode)
        {
            return SysUserDA.SelectUserProcess(strCode);
        }

    }

}
